package com.xplay.xpocker.room.mahjong.action;

import com.xplay.xpocker.constant.MessageNoticeEnum;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.entity.GameRuMahjongLogs;
import com.xplay.xpocker.room.Message;
import com.xplay.xpocker.room.RoomInfoTL;
import com.xplay.xpocker.room.RoomStrategy;
import com.xplay.xpocker.room.mahjong.constant.MKey;
import com.xplay.xpocker.room.mahjong.dto.*;
import com.xplay.xpocker.room.mahjong.AbstractMahjongMessageStrategy;
import com.xplay.xpocker.room.message.MessageContent;
import com.xplay.xpocker.room.message.MessageContentFactory;
import com.xplay.xpocker.room.message.MessageToClient;
import com.xplay.xpocker.room.message.MessageType;
import com.xplay.xpocker.util.JacksonStringUtil;
import com.xplay.xpocker.dvo.AbstractRoom;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PrepareAction extends AbstractMahjongMessageStrategy<AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs>, MahjongMessage> {

    @Override
    protected String strategyName() {
        return MahjongActionEnum.READING.getAction();
    }

    @Override
    public boolean doMessage(AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs> roomInfo, MahjongMessage messageInfo) {
        GameRuMahjongUserState currentUser = roomInfo.getGameInfo().getCurrentUser();
        List<MessageContent<MessageToClient>> messageList = MessageContentFactory.
                init(roomInfo.getCode(), MessageType.ACTION.getType(), currentUser.getUserId(), MessageNoticeEnum.ALL)
                .multi(MKey.exeActionUid, currentUser.getUserId(), MKey.type, MahjongActionEnum.READING.getAction()).send().getMessageList();
        RoomInfoTL.addMessageToClient(messageList);
        if (!checkExistsWaitCompleted(MahjongActionEnum.READING) && roomInfo.getGameInfo().getAllUser().size() == roomInfo.getRule().getUserCount()) {
            roomInfo.setSeveralInning(roomInfo.getSeveralInning() + 1);
            //给所有用户发牌
            dealCards();
            // 判断是否开启了换三张
            roomInfo.setStatus(MahjongStatusEnum.GAMING.getStatus());
            if (roomInfo.getRule().checkExistsRule(RuleConstant.HSZ)) {
                for (GameRuMahjongUserState userInfo : roomInfo.getGameInfo().getAllUser()) {
                    userInfo.setWaitCompleted(MahjongActionEnum.EXCHANGE.getAction());
                }
            } else {
                // 没有开启换三张 给庄家添加出牌操作
                getBanker().setWaitCompleted(MahjongActionEnum.EXPORT_CARD.getAction());
            }
            for (GameRuMahjongUserState userInfo : roomInfo.getGameInfo().getAllUser()) {
                // 用当前用户反查房间信息  进行脱敏
                AbstractRoom<?, ?, ?> desensitize = RoomStrategy.getRoomStrategy(roomInfo.getCode()).desensitize(roomInfo, roomInfo.getCode(), userInfo.getUserId());
                List<MessageContent<MessageToClient>> startMessage = MessageContentFactory.
                        init(roomInfo.getCode(), MessageType.ACTION.getType(),
                                userInfo.getUserId(), MessageNoticeEnum.CURRENT)
                        .multi(MKey.exeActionUid, userInfo.getUserId(),
                                MKey.type, MahjongActionEnum.STARTING_GAME.getAction(),
                                MKey.data, JacksonStringUtil.objectToString(desensitize)
                        )
                        .send().getMessageList();
                RoomInfoTL.addMessageToClient(startMessage, 1);
            }
        }
        return true;
    }

}
